package cuiyt.leetcode.dichotomy.demod;

/**
 * @author cyt
 * @create 2020-10-09 14:56
 */
public class Tb {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Tb tb = new Tb();
        System.out.println(tb.isPerfectSquare(9));
    }

    /**
     * 判断是否为完全平方数
     *
     * @param num
     * @return
     */
    public boolean isPerfectSquare(int num) {
        if (num < 2) {
            return true;
        }
        long l = 2;
        long r = num / 2;
        while (l <= r) {
            long mid = l + (r - l) / 2;
            long res = mid * mid;
            if (res == num) {
                return true;
            }
            if (res > num) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return false;
    }
}
